home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / viewkit / VCal / path.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.5 KB  |  108 lines

  1. /*
  2.  * Copyright (C) 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <malloc.h>
  20. #include <string.h>
  21. #include <pwd.h>
  22. #include "path.h"
  23.  
  24. char*
  25. pathdirname(const char* path)
  26. {
  27.     register char*    c;
  28.     char*        lastslash = 0;
  29.     char*        retstr;
  30.  
  31.     for (c = (char *) path; *c; c++) {
  32.     if (*c == '/') {
  33.         char* tmplastslash = c;
  34.         for (c++; *c == '/'; c++) {;}
  35.                     /* ignore redundant slashes */
  36.         if (*c) {        /* not superfluous trailing slash */
  37.         lastslash = tmplastslash;
  38.         } else {        /* was superfluous trailing slash */
  39.         break;
  40.         }
  41.     }
  42.     }
  43.  
  44.     if (lastslash == 0) {
  45.     if (path[0] == '/') {     /* path is "/" or has repeated "/" */
  46.         retstr = strdup("/");
  47.         return retstr;
  48.     } else {        /* basename only implies current dir "." */
  49.         retstr = strdup(".");
  50.         return retstr;
  51.     }
  52.     }
  53.  
  54.  
  55.     retstr = (char *) malloc(lastslash-path+1);
  56.     strncpy(retstr, path, lastslash-path);
  57.                 /* copy only dir sans trailing /'s */
  58.     retstr[lastslash-path] = '\0';
  59.  
  60.     return retstr;
  61. }
  62.  
  63.  
  64. char*
  65. pathexpandtilde(const char* path)
  66. {
  67.     register char*    c;
  68.     char        user[255];
  69.     char*        homedir;
  70.     char*        retstr;
  71.  
  72.     if (path[0] != '~') {
  73.     retstr = strdup(path);
  74.     return retstr;
  75.     }
  76.     
  77.     if (path[1] == '/' || path[1] == '\0') {    /* ~/... or ~ */
  78.     homedir = getenv("HOME");
  79.     c = (char *) &(path[1]);
  80.     } else {                    /* ~<user>/... */
  81.     struct passwd*    pwent;
  82.     int i;
  83.  
  84.     c = (char *) &(path[1]);
  85.     for (i=0; *c != '/' && *c != '\0'; c++, i++) {
  86.         user[i] = *c;
  87.     }
  88.     user[i] = '\0';
  89.  
  90.     pwent = getpwnam(user);
  91.     if (pwent == 0) {            /* no such user */
  92.         retstr = strdup(path);
  93.         return retstr;
  94.     }
  95.  
  96.     homedir = pwent->pw_dir;
  97.     }
  98.  
  99.     if (homedir == 0) {
  100.     homedir = "";                /* consistency with csh & ksh */
  101.     }
  102.     retstr = (char *) malloc(strlen(homedir) + strlen(c) + 1);
  103.     strcpy(retstr, homedir);
  104.     strcat(retstr, c);
  105.     return retstr;
  106. }
  107.  
  108.